<script setup>
import { ref, computed } from 'vue'
const cartList = ref([
    { "id": "4026450", "name": "龙凤玉环", "picture": "https://pic.imgdb.cn/item/6640b6ad0ea9cb14035bc4c9.png", "price": "7900.00", "count": 1, selected: true },
    { "id": "4026450", "name": "龙凤玉环", "picture": "https://pic.imgdb.cn/item/6640b6ad0ea9cb14035bc4c9.png", "price": "7900.00", "count": 1, selected: true },
    { "id": "4026450", "name": "龙凤玉环", "picture": "https://pic.imgdb.cn/item/6640b6ad0ea9cb14035bc4c9.png", "price": "7900.00", "count": 1, selected: true },
   
]);

const isAll = computed(() => cartList.value.every((item) => item.selected))
console.log(isAll.value);

//全选
const allCheck = (selected) => {
    cartList.value.forEach(item => item.selected = selected)
}
// const delCart = (item) => {
//     console.log(item);
// }

//总的数量，所有项的count之和
const allCount = computed(() =>
    cartList.value.reduce((a, c) => a + c.count, 0)
);
//已选择的数量
const selectedCount = computed(() =>
    cartList.value
        .filter((item) => item.selected)
        .reduce((a, c) => a + c.count, 0)
);
//已选择商品价格合计
const selectedPrice = computed(() =>
    cartList.value
        .filter((item) => item.selected)
        .reduce((a, c) => a + c.count * c.price, 0)
);
</script>

<template>
    <div class="xtx-cart-page">
        <div class="container m-top-20">
            <div class="cart">
                <table>
                    <thead>
                        <tr>
                            <th width="120">
                                <!-- 全选框 -->
                                <a-checkbox v-model:checked="isAll" @change="allCheck" />
                            </th>
                            <th width="400">商品信息</th>
                            <th width="220">单价</th>
                            <th width="180">数量</th>
                            <th width="180">小计</th>
                            <th width="140">操作</th>

                        </tr>
                    </thead>
                    <!-- 商品列表 -->
                    <tbody>
                        <tr v-for="i in cartList" :key="i.id">
                            <td>
                                <!-- 单选框 -->
                                <a-checkbox v-model:checked="i.selected" @change="singleCheck(i.id, $event)" />
                            </td>
                            <td>
                                <div class="goods">
                                    <RouterLink to="/"><img :src="i.picture" alt="" /></RouterLink>
                                    <div>
                                        <p class="name ellipsis">
                                            {{ i.name }}
                                        </p>
                                    </div>
                                </div>
                            </td>
                            <td class="tc">
                                <p>&yen;{{ i.price }}</p>
                            </td>
                            <td class="tc">
                                <a-input-number v-model:value="i.count" />

                            </td>
                            <td class="tc">
                                <p class="f16 red">&yen;{{ (i.price * i.count).toFixed(2) }}</p>
                            </td>
                            <td class="tc">
                                <p>
                                    <a-popconfirm title="确认删除吗?" ok-text="确认" cancel-text="取消" @confirm="delCart(i)">
                                        <a href="javascript:;">删除</a>
                                    </a-popconfirm>

                                </p>
                            </td>
                        </tr>
                        <tr v-if="cartList.length === 0">
                            <td colspan="6">
                                <div class="cart-none">
                                    <a-empty description="购物车列表为空">
                                        <a-button type="primary">随便逛逛</a-button>
                                    </a-empty>

                                </div>
                            </td>
                        </tr>
                    </tbody>

                </table>
            </div>
            <!-- 操作栏 -->
            <div class="action">
                <div class="batch">
                    共 {{ allCount }} 件商品，已选择 {{ selectedCount }} 件，商品合计：
                    <span class="red">¥ {{ selectedPrice }}</span>
                </div>
                <div class="total">
                    <a-button size="large" type="primary" @click="$router.push('/checkout')">下单结算</a-button>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.xtx-cart-page {
    margin-top: 20px;

    .cart {
        background: #fff;
        color: #666;

        table {
            border-spacing: 0;
            border-collapse: collapse;
            line-height: 24px;

            th,
            td {
                padding: 10px;
                border-bottom: 1px solid #f5f5f5;

                &:first-child {
                    text-align: left;
                    padding-left: 30px;
                    color: #999;
                }
            }

            th {
                font-size: 16px;
                font-weight: normal;
                line-height: 50px;
            }
        }
    }

    .cart-none {
        text-align: center;
        padding: 120px 0;
        background: #fff;

        p {
            color: #999;
            padding: 20px 0;
        }
    }

    .tc {
        text-align: center;

        a {
            color: #27ba9b;
        }

        .xtx-numbox {
            margin: 0 auto;
            width: 120px;
        }
    }

    .red {
        color: #cf4444;
    }

    .green {
        color: #27ba9b;
    }

    .f16 {
        font-size: 16px;
    }

    .goods {
        display: flex;
        align-items: center;

        img {
            width: 100px;
            height: 100px;
        }

        >div {
            width: 280px;
            font-size: 16px;
            padding-left: 10px;

            .attr {
                font-size: 14px;
                color: #999;
            }
        }
    }

    .action {
        display: flex;
        background: #fff;
        margin-top: 20px;
        height: 80px;
        align-items: center;
        font-size: 16px;
        justify-content: space-between;
        padding: 0 30px;

        .xtx-checkbox {
            color: #999;
        }

        .batch {
            a {
                margin-left: 20px;
            }
        }

        .red {
            font-size: 18px;
            margin-right: 20px;
            font-weight: bold;
        }
    }

    .tit {
        color: #666;
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
    }

}
</style>